package Client;

import model.Student;
import model.User;
import model.UserMassage;
import util.ClientScanner;
import util.Io;

import java.io.IOException;
import java.util.ArrayList;

/**
 * 学生管理系统界面设计及主要功能
 */
public class StudentClinetView {
    /**
     * 使用合成复用原则
     */

    // 合成复用原则
    private static ArrayList<Student> ls;
    public StudentClinetView(ArrayList<Student> ls){
        this.ls = ls;
    }
    // 学生主页面的方法
    public static void studentClinetMainPage(ClientInitClose cic) throws IOException, ClassNotFoundException, InterruptedException {
        UserMassage massage = new UserMassage("studentCheck");
        // 将UserMassage对象通过输出流发送给服务器
        cic.getOos().writeObject("学生登录:"+massage);
        // 定义io流来读文件
        Io io = new Io(ls);
        System.out.println(io.read());


        Function f = new Function();
        while(true){
            System.out.println("\n\n\t\t 学生系统");
            System.out.println("----------------------------");
            System.out.print("   [1] 增加学生");
            System.out.println("   [2] 删除学生");
            System.out.print("   [3] 修改学生");
            System.out.println("   [4] 查找学生");
            System.out.print("   [5] 遍历学生");
            System.out.println("   [0] 退出系统");
            System.out.println("----------------------------");
            System.out.println("请输入你要操作的业务");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在增加学生。。。");
                    f.add();

                    break;
                case 2:
                    System.out.println("正在删除学生。。。请输入id");
                    int a = ClientScanner.getScanner().nextInt();
                    f.delete(a);

                    break;
                case 3:
                    System.out.println("正在修改学生。。。请输入id");
                    int b = ClientScanner.getScanner().nextInt();
                    f.update(b);

                    break;
                case 4:
                    System.out.println("正在查找学生。。。请输入id");
                    int c = ClientScanner.getScanner().nextInt();
                    f.query(c);

                    break;
                case 5:
                    System.out.println("正在遍历学生。。。");
                    Thread.sleep(2000);
                    for (Student student:f.ls) {
                        System.out.println(student);
                    }
                    break;
                case 0:
                    System.out.println("正在退出系统。。。");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    io.write(ls);
                    return;
                default:

                    System.out.println("输入错误,请重新输入！");
            }
            // System.out.println(f.ls.toString()); //空指针异常

        }

        // io.write(ls); 错误
    }


}
